package com.example.dellpc.myapplication;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by 刘智强 on 2016/9/18.
 * 自动轮播和无限滑动的viewpager
 */
public class AutoViewpagerActivity extends Activity {
    @ViewInject(R.id.viewpager)
    private ViewPager viewPager;
    @ViewInject(R.id.tv_index)
    private TextView tv_index;

    private List<View> views;
    private List<String> urls;
    private int index = 0;

    private int currentPage = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            viewPager.setCurrentItem(index % 5);
            Log.e("TAG", index % 5 + "");
        }
    };
    private TimerTask timerTask;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_viewpager);
        Fresco.initialize(getApplicationContext());
        ViewUtils.inject(this);
        initViewPager();

    }

    public void initViewPager() {

        views = new ArrayList<>();
        urls = new ArrayList<>();
        urls.add("http://pic1a.nipic.com/2009-03-05/200935145839443_2.jpg");
        urls.add("http://pic9.nipic.com/20100909/2531170_071048737499_2.jpg");
        urls.add("http://pic9.nipic.com/20100813/2531170_123456384599_2.jpg");
        urls.add("http://img3.imgtn.bdimg.com/it/u=2610939267,2111867656&fm=21&gp=0.jpg");
        urls.add("http://img4.imgtn.bdimg.com/it/u=978712835,1165639973&fm=21&gp=0.jpg");


        for (int i = 0; i < 5; i++) {
            SimpleDraweeView view = new SimpleDraweeView(getApplicationContext());
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setImageURI(Uri.parse(urls.get(i)));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            views.add(view);
        }


        tv_index.setText("1/" + views.size());
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.setCurrentItem(1000 * views.size());
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                tv_index.setText(position + 1 + "/" + views.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        viewPager.setCurrentItem(0);
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
                index++;
            }
        };
        timer.schedule(timerTask, 3000, 3000);


    }


    class MyPagerAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            ((ViewPager) container).removeView(views.get(position%views.size()));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(views.get(position % views.size()));
            return views.get(position % views.size());
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }
    }
}
